const computedBehavior = require("miniprogram-computed").behavior;
Page({
    behaviors: [computedBehavior],
    data: {
        goodsList: [],
        prices: []
    },
    // 接口的返回数据
    computed: {
        return(e) {
            console.log(e)
        },
        getPrice(data) {
            console.log(data)
            // 注意： computed 函数中不能访问 this ，只有 data 对象可供访问
            // 这个函数的返回值会被设置到 this.data.sum 字段中
            return data.toFixed(2);
        },
    },
    onShow() {
        let that = this
        let getGoodsInfo = new Promise((resolve, reject) => {
            wx.request({
                url: "https://www.cbing.icu:8095/wxUser/getAllTravel",
                method: "GET",
                success(res) {
                    let prices = []
                    for (let i in res.data.data) {
                        let price = res.data.data[i].price
                        prices[i] = price.toFixed(2)
                    }
                    that.setData({
                        goodsList: res.data.data,
                        prices: prices
                    })
                    resolve()
                }
            })
        })


    },

    handleItemTap(e) {
        let id = e.target.dataset.index
        let list = this.getCurrList(id)
        console.log(list)
        this.setData({
            currList: list,
            currCate: id
        })
    },
    getCurrList(id) {
        let list = []
        for (let item of this.data.goodsList) {
            if (item.categoryid === id) {
                list.push(item)
            }
        }
        return list
    },
    toDetail(e) {
        let that = this
        console.log(e)
        wx.navigateTo({
            url: "../goods_list/index",
            success: (res) => {
                // 通过 eventChannel 向被打开页面传送数据
                res.eventChannel.emit('acceptDataFromOpenerPage', {data: that.data.goodsList[e.currentTarget.dataset.index]})
            }
        })
    },
    addToCart(e) {
        let token = wx.getStorageSync("token")
        let that = this
        wx.showModal({
            title: 'Tips',
            content: '您确定要将此商品加入购物车吗',
            confirmText: '确定',
            cancelText: '取消',
            success: function (res) {
                if (res.cancel) {

                } else {
                    let formdata = '\r\n--XXX' +
                        '\r\nContent-Disposition: form-data; name="goodId"' +
                        '\r\n' +
                        '\r\n' + that.data.currList[e.currentTarget.dataset.index].id +
                        '\r\n--XXX' +
                        '\r\nContent-Disposition: form-data; name="num"' +
                        '\r\n' +
                        '\r\n1' +
                        '\r\n--XXX--'
                    wx.request({

                        url: "https://www.cbing.icu:8095/wxUser/addToShopCart",
                        method: 'POST',
                        header: {
                            'content-type': 'multipart/form-data;boundary=XXX',
                            'Authorization': token
                        },
                        data: formdata,
                        success(res) {

                            wx.showToast({
                                icon: 'none',
                                title: res.data.message,
                                duration: 2000

                            })

                        }
                    })
                }
            }
        })
    },
})